-- Building
-- Create by weism
-- 建筑物对象

Building = {
    x = 0,
    y = 0,
    rid = "",
    
    -- 小建筑群信息
    group = -1, 
    smallBuildings = nil,
};
Building.__index = Building;

-- 构造函数
function Building.new(dbase)
    local self = {};
    setmetatable(self, Building);
    
    -- 设置其位置、基础的建筑信息
    self.x = dbase.x;
    self.y = dbase.y;
    self.rid = dbase.rid;
    self.base = BuildingM.find(dbase.class_id);
    
    -- 登记本对象
    RID.add(self.rid, self);
    
    return self;
end

-- 取得该对象的描述字符串
function Building:get_ob_id()
    return string.format("Building:%s", tostring(self.rid));   
end

-- 析构函数
function Building:destroy()
    RID.remove(self.rid);
    self = {};
end

-- 取得周围所有的小建筑物
function Building:getSmallBuildings()
    local r = {};
    local groupInfo = SmallBuildingM.getGroup(self.group);
    local index = 1;
    for i = 1, #groupInfo do
    repeat
    	if self.smallBuildings ~= nil then
    	   local amount = Buffer.get8(self.smallBuildings, index);
    	   if amount > 0 then 
    	       index = index + 1;
    	       break;
    	   end
    	end
    	
    	local node = groupInfo[i];
    	table.insert(r, table.add({ index = index }, node));
    	index = index + 1;
    until true
    end
    
    return r;
end
